// pages/admin-order/admin-order.js
var util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hiddenName: true,
    index: 0,
    default: '请选择快递公司',
    hiddenmodalput: true,
    navState: 0, //导航状态
    hiddensearch: true,

  },
  //点击导航
  navSwitch: function (e) {
    // console.log(e.currentTarget.dataset.index)
    let index = e.currentTarget.dataset.index;
    this.setData({
      navState: index
    })
  },

  bindPickerChange: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    let index = e.detail.value
    let array = this.data.arrtrack
    this.setData({
      index: index,
      default: array[index].tracking
    })
  },


  deliver: function (e) {
    var id = e.currentTarget.dataset.id
    var condition = e.currentTarget.dataset.condition
    if (condition == '待发货') {
      console.log("未发货")
      this.setData({
        id: id,
        hiddenmodalput: !this.data.hiddenmodalput,
      })
    } else {
      console.log("已经发货")
      wx.showModal({
        title: '',
        content: '已经发货了！',
      })
    }
  },
  cancelM: function (e) {
    this.setData({
      hiddenmodalput: true,
    })
  },

  confirmM: function (e) {
    let key = this.data.arrtrack[this.data.index].tracking;
    console.log(key)

    let trackingname = this.data.arrtrack[this.data.index].trackingname;
    var num = Math.floor(Math.random() * 10000000000 + 10000000000)
    var trackingnumber = trackingname + num
    console.log(trackingnumber)

    var time = util.formatTime(new Date());
    console.log(time)
    var that = this;
    wx.request({
      url: 'http://localhost/shopping/admin_order_edit.php',
      header: {
        'Content-Type': 'application/json'
      },
      data: {
        id: that.data.id,
        enddate: time,
        tracking: key,
        trackingnumber: trackingnumber
      },
      success: function (res) {
        // console.log(res.data)
        that.Order();
        wx.showToast({
          title: '成功发货！',
          icon: 'success',
          duration: 2000
        })
        that.setData({
          arr: res.data.orders,

        })
      },
      fail: function (res) {
        console.log("fail");
      }
    })
    this.setData({
      hiddenmodalput: true
    })
  },

  bindKeyInput: function (e) {
    this.setData({
      key: e.detail.value
    })
  },
  search: function (e) {
    // console.log(e)
    let that = this;
    // console.log(that.data.key);
    wx.request({
      url: 'http://localhost/shopping/admin_order_search.php',
      header: {
        'Content-Type': 'application/json'
      },
      data: {
        key: that.data.key,
      },
      success: function (res) {
        console.log(res.data);
        that.setData({
          arrsearch: res.data.orderssearch,
          hiddenName: !that.data.hiddenName,
          hiddensearch: !that.data.hiddensearch,
        })
      },
      fail: function (res) {
        console.log("fail");
      }
    })

  },


  modal_info(e) {
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/admin-order-info/admin-order-info?id=' + id + '',
    })
  },
  Order: function () {
    var that = this;
    wx.request({
      url: 'http://localhost/shopping/admin_order_deliver.php',
      header: {
        'Content-Type': 'application/json'
      },
      data: {},
      success: function (res) {
        console.log(res.data)
        that.setData({
          arrdeliver: res.data.goodsdeliver,

        })
      },
      fail: function (res) {
        console.log("fail");
      }
    })
    wx.request({
      url: 'http://localhost/shopping/admin_order_all.php',
      header: {
        'Content-Type': 'application/json'
      },
      data: {},
      success: function (res) {
        console.log(res.data)
        that.setData({
          arrall: res.data.goodsall,
        })
      },
      fail: function (res) {
        console.log("fail");
      }
    })
    wx.request({
      url: 'http://localhost/shopping/tracking.php',
      header: {
        'Content-Type': 'application/json'
      },
      data: {},
      success: function (res) {
        console.log(res.data)
        that.setData({
          arrtrack: res.data.tracking,

        })
      },
      fail: function (res) {
        console.log("fail");
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.Order();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})